from selenium import webdriver
from selenium.webdriver import ChromeOptions
#导入By类
from selenium.webdriver.common.by import By
#导入显示等待类
from selenium.webdriver.support.ui import WebDriverWait
#导入期望场景类
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support import expected_conditions as EC

import time
from lxml import etree
import os

chromeDriverPath='../chromedriver_win32/chromedriver.exe'
accountAddress='127.0.0.1:9222'
url_path='https://cp.kuaishou.com/profile'
bro=None
header = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"}

if __name__=='__main__':
    rootdir='F:/filedata/2023/07/24/video/youtube'
    list=os.listdir(rootdir)
    for i in range(0,len(list)):
        path=os.path.join(rootdir,list[i])
        if os.path.isfile(path=path):
            # print(path)
            if path.endswith(".mp4"):
                #time.sleep(5)
                print(path)
                video_path=path
                #加载cookies中已经保存的账号和密码
                options = ChromeOptions()
                options.add_experimental_option("debuggerAddress", accountAddress)
                bro=webdriver.Chrome(executable_path=chromeDriverPath,chrome_options=options)
                print("开始")
                bro.get(url_path)
                #time.sleep(5)
                wait=WebDriverWait(bro,60,poll_frequency=0.5,ignored_exceptions=None)

                #div_btn=bro.find_element(By.XPATH,'//*[@id="app"]/div[1]/div[1]/div/section/ul/div')
                #div_btn.click()
                div_btn=wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="app"]/div[1]/div[1]/div/section/ul/div')))
                print('div_btn=',div_btn)
                div_btn.click()
                print('点击高清上传按钮！！！')
                #upload_btn=wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="onvideo_creator_platform"]/div/section/div[2]/div[1]/div[2]/button')))
                #upload_btn.click()
                input_file=wait.until(EC.presence_of_element_located((By.XPATH,'//*[@id="onvideo_creator_platform"]/div/input')))
                print(input_file)
                input_file.send_keys(video_path)
                tmpnum=0
                while True:
                    time.sleep(1)
                    page_text=bro.page_source
                    tree=etree.HTML(page_text)
                    upload_status_text=tree.xpath('//*[@id="onvideo_creator_platform"]/div/div[3]/div/div[2]/div/div[2]//text()')
                    #upload_status_div=bro.find_element(By.XPATH,'//*[@id="onvideo_creator_platform"]/div/div[3]/div[2]/div[2]/div/div[2]')
                    print(upload_status_text)
                    if upload_status_text:
                        upload_status_text=upload_status_text[0]
                    if '上传成功'==upload_status_text:
                        break
                    if tmpnum>600:
                        break
                    tmpnum=tmpnum+1
                print('上传完成')
                fabu_div=wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="onvideo_creator_platform"]/div/div[3]/div/div[4]/div[2]/div[7]/button[1]')))
                fabu_div.click()
                bro.close
                print('完成close')
                os.rename(path,path.replace('.','-d.'))
                print("结束")
